// pages/news/detail.js
const util = require('../../utils/util.js');
const apiUrl = require('../../utils/apiUrl.js');
var http = require('../../utils/httpHelper.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    winWidth: 0,
    winHeight: 0,
    fontSize: 16,
    id: 0,
    detail: {},
    user: {},
    article_content: {},
    loaddinghidden: true,
    showLoading: false,
    check_status:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 页面初始化 options 为页面跳转所带来的参数
    var that = this;
    var id = options.id;
    if (!id) {
      wx.showModal({
        title: '加载失败！',
        success: function (res) {
          if (res.confirm) {
            wx.navigateBack();
          }
        }
      })
      return;
    }

    /**
     * 获取系统信息
     */
    wx.getSystemInfo({

      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
    var user = wx.getStorageSync("user");

    if (user) {
      that.setData({
        user: user,
      });
    }

    // 显示 loading
    that.setData({
      id: id,
      loaddinghidden: false
    });

    that.loadData();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var fontSize = wx.getStorageSync("fontSize");

    if (fontSize) {
      this.setData({
        fontSize: fontSize,
      });
    }
  },
  loadData: function () {
    var that = this;
    var parameter = {};
    parameter['id'] = that.data.id;
    //获取数据
    http.ApiGet(apiUrl.UrlNewsDetail, parameter, function (res) {
      wx.stopPullDownRefresh() //停止下拉刷新
      if (res.code == 1) {
        
        var article_content = res.data.content.replace(/<img/gi, '<img style="max-width:100%;height:auto;display:block;padding-top:2px;" ')
          .replace(/<section/g, '<div')
          .replace(/\/section>/g, '\div>');

        var check_status="";
        if (res.data.check_status == 99) {
          check_status = "已发布";
        } else if (res.data.check_status == 1) {
          check_status = "一审";
        } else if (res.data.check_status == 2) {
          check_status = "二审";
        } else if (res.data.check_status == 3) {
          check_status = "三审";
        } else if (res.data.check_status == 4) {
          check_status = "四审";
        } else if (res.data.check_status == -99) {
          check_status = "草稿";
        } else {
          check_status = "退稿";
        }

        that.setData({
          loaddinghidden: true,
          detail: res.data,
          article_content: article_content,
          check_status: check_status
        });
      } else {
        wx.showModal({
          title: res.msg
        });
      }
    });
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.loadData();
  },
  imageTap: function () {
    var that = this;
  
  },
  onShareAppMessage: function () {
    // 用户点击右上角分享
    var link = "https://www.sogx.cn/wap/news/detail/id/" + this.data.id +"/is_app/1";
    var user = wx.getStorageSync("user");
    return {
      title: " ", //this.data.detail.title, // 分享标题
      desc: user.nickname, // 分享描述
      path: '/pages/webview/index?url=' + link // 分享路径
    }
  }
})